<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;
use Think\Page;
class LateController extends BaseController{
    public function add(){    
        $relation=D('Station')
        ->relation(true)
        ->select();
        $this->assign('relationlist',$relation);     
        $this->assign('monthlist',D('Month')->select());
        $nowmonth=date('m');
        $this->assign('nowmonth', $nowmonth);  
        $this->assign('title','迟到记录页面');
        $this->display('Late_info');
    }
 public function insert(){
             
        $where['id']=I('post.latename');  
        $staffname=D('Staff')->where($where)->find();
        
        $data['latemonth']=I('post.latemid');
        $data['latetime']=I('post.latetime');
        $data['latename']=$staffname['name'];
        $data['latemid']=I('post.latemid');
        
        $late['staff_name']= $staffname['name'];
        $late['month']= I('post.latemid');
        
        $tresult=D('Ture')->where($late)->find();
        $res['tasklate']=I('post.latetime')+ $tresult['tasklate'];
        
        $rea['month']=I('post.latemid');
        $rea['tasklate']=I('post.latetime');
        $rea['staff_name']= $staffname['name'];
             
        if ($tresult>0){
            D('Ture')->where($late)->save($res);
        }else {
            D('Ture')->add($rea);
        }     
        $map['id']=I('post.latename');  
        $staffresult=D('staff')->where($map)->find();
        $staff['late']=$staffresult['late']+I('post.latetime');                     
        if (D('Late')->add($data)){
            if (D('staff')->where($map)->save($staff)){
                $data['state']=1;
                $data['msg']='提交成功';
                $this->ajaxReturn($data);
            }else {
                $data['state']=0;
                $data['msg']='提交失败';
                $this->ajaxReturn($data);
            }
        }else {
            $data['state']=0;
            $data['msg']='提交失败';
            $this->ajaxReturn($data);
        }
             
         
    }
    
    public function View(){
        
        $a = $_GET['tex'];
        if ($a == null) {
            $a = date('m');
            if ($a < 10) {
                $a = substr($a, 1);
            }
        } else {
            $a = $_GET['tex'];
        }
        
        $where['latemid'] = $a;
        
      
        $ss = D('Late');
        $count = $ss->count();
        $page = new Page($count, 20);
        $page->setConfig('first', '首页');
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $page->setConfig('last', '末页');
        $page->rollPage = '6';
        $page->setConfig('theme', '%HEADER%  共    %TOTAL_PAGE% 页   %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        
        
        $result=D('Late')
        ->where($where)
        ->limit($page->firstRow, $page->listRows)
        ->order('convert(latename using utf8) desc')
        ->select(); 
        
        $this->assign('month', $a);
        $this->assign('year', date('Y'));
        $this->assign('null', null);
        $this->assign('monthlist', D('Month')->select());
        $this->assign('page', $show);
        $this->assign('title','迟到列表页面');
        $this->assign('info','添加');
        $this->assign('latelist',$result);
        $this->display('Late_list');
    }
    
    public function update(){
        $data['latestatus']=0;
        $where['id']=I('get.id');
        $data['tasklate']=0;
        if(D('Task')->where($where)->save($data)){
            $data['state']=1;
            $data['msg']='删除成功';
            $this->ajaxReturn($data);
        }else {
            $data['state']=0;
            $data['msg']='删除失败';
            $this->ajaxReturn($data);
        }
         
         
    }
    
    public function edit(){
        $relation=D('Station')
        ->relation(true)
        ->select();
        $this->assign('relationlist',$relation);
        $this->assign('monthlist',D('Month')->select());
        $this->assign('info','修改');
        $this->assign('title','迟到编辑页面');
        $where['id']=I('get.id');
        $this->assign('editlist',D('Late')->where($where)->find());
        $this->display('Late_info');
    }
    
    public function updatelate(){
        $where['id']=I('post.id');
        $late['late']=I('post.late');
        
     
        $post['latemid']=I('post.latemid');
       
        $post['latetime']=I('post.latetime');
       
        $zxlate=D('Late')->where($where)->find();
        $post['latename']=$zxlate['latename'];
        $map['month']=I('post.latemid');
        $map['staff_name']=$zxlate['latename'];
        
        $zxresult=D('Ture')->where($map)->find();
        if (!$zxresult){
            $this->error('相应月份里面没有该员工！');
        }else {
            $data['tasklate']=$zxresult['tasklate']+$post['latetime']-$late['late'];         
            $data['month']=I('post.latemid');
            if (!D('Late')->where($where)->find()){
                $this->error('不能修改员工人员！');
            }else {
                          
            $zres=D('Late')->where($where)->save($post);            
            $read=D('Ture')->where($map)->save($data);
            
            if (false !==$zres||0 !==$zres){
                if (false !==$read ||0 !==$read){
                    $this->success('修改成功！',U('Admin/Late/View'));
                }else {
                    $this->error('修改失败！');
                }
            }else {
                $this->error('修改失败！');
            }
            
            
             }   
            
        }
    }
    
    
    
    
    public  function lateb(){
        if ($_GET['tex'] > 0) {
            $data['state'] = 1;
            $data['d'] = $_GET['tex'];
            $this->ajaxReturn($data);
        } else {
            $data['state'] = 0;
            $data['msg'] = '请求超时，请重试！';
            $this->ajaxReturn($data);
        }
    }
    
    public function cdgl(){
        $cdid = $_POST['select'];
        $map['id'] = array('in',  $cdid );
        if(D('Late')->where($map)->delete()){
            $data['state']=1;
            $data['msg']='批量删除成功';
            $this->ajaxReturn($data);
        }else {
            $data['state']=0;
            $data['msg']='提交失败';
            $this->ajaxReturn($data);
        }
    }
    
    
    
    
}